package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.DataConf;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.imps.BaseTextChangedListener;
import com.yjhb.android.sbz.tool.Tool;
import com.yjhb.android.sbz.v2.mvp.presenter.ConfigPresenter;
import com.yjhb.android.sbz.v2.mvp.view.ConfigView;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;

import org.jetbrains.annotations.Nullable;
import org.sufficientlysecure.htmltextview.HtmlTextView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * 创建者 zx
 * 创建时间 2020/12/10 15:17
 */
public class MainPageToppingDialog extends BaseDialog {
    private final String TAG = "MainPageToppingDialog";

    @BindView(R.id.input_et)
    EditText input_et;
    @BindView(R.id.consumeMoney_tv)
    TextView consumeMoney_tv;
    @BindView(R.id.rule_tv)
    HtmlTextView rule_tv;
    @BindView(R.id.last_time_tv)
    TextView last_time_tv;

    private ConfigPresenter configPresenter;

    public MainPageToppingDialog(@NonNull Activity context) {
        super(context);
    }

    @Override
    protected void init() {
        configPresenter = new ConfigPresenter(new ConfigView() {
            @Override
            public void onConfig(@Nullable JSONObject config) {
                if (config!=null){
                    rule_tv.setHtml(config.getString("rule"));
                }
            }

            @Override
            public void onConfig(@Nullable JSONArray config) {

            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
        configPresenter.getSysConfig("TaskFlushRule");
        setMoney("0.00");
        if (App.userInfo.getCouponNum() > 0) {
            last_time_tv.setVisibility(View.VISIBLE);
            last_time_tv.setText("当前赠送剩余时长:" + App.userInfo.getCouponNum() + "小时");
        } else {
            last_time_tv.setVisibility(View.GONE);
        }

        input_et.addTextChangedListener(new BaseTextChangedListener() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s == null) return;
                if (s.length() > 5) {
                    s.delete(5, s.length());
                }
                if (s.toString().contains(".")) {
                    String content = s.toString().replace(".", "");
                    input_et.setText(content);
                    input_et.setSelection(content.length());
                }

            }
        });
    }

    public Integer getHour() {
        try {
            if (Tool.isEditEmpty(input_et)) return null;
            return Integer.parseInt(input_et.getText().toString());
        } catch (Exception e) {
        }
        return null;
    }

    private void setMoney(String money) {
        CharSequence consume = ZxSpanTool.ColorSpan("消费金额" + money + "元", 4, 4 + money.length(),
                getContext().getResources().getColor(R.color.red_3b));
        consumeMoney_tv.setText(ZxSpanTool.ZoomSpan(consume, 4, 4 + money.length(), 1.2f));
    }

    private BigDecimal calculate() {
        try {
            //单价
            BigDecimal price = new BigDecimal(DataConf.RECOMMEND_PRICE);
            price = price.multiply(Tool.getMyVipInfo().getRecommendDiscount());
            return new BigDecimal(input_et.getText().toString()).multiply(price)
                    .setScale(BigDecimal.ROUND_HALF_UP, 2);
        } catch (Exception e) {
            ZxLogTool.err(e);
        }
        return null;
    }

    @OnTextChanged(R.id.input_et)
    void onTextChanged() {
        if (Tool.isEditEmpty(input_et)) {
            setMoney("0");
            return;
        }
        BigDecimal money = calculate();
        if (money != null) {
            setMoney(Tool.getPrettyNumber(money).toString());
        } else {
            setMoney("0.00");
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_main_page_topping;
    }

    @OnClick({R.id.close_iv, R.id.confirm_tv})
    void click(View view) {
        if (onClickListener != null) {
            if (view.getId() == R.id.close_iv)
                onClickListener.onClick(this, BUTTON_CANCEL);
            else if (view.getId() == R.id.confirm_tv) {
                onClickListener.onClick(this, BUTTON_SUBMIT);
            }
        }
    }
}
